package com.ruoyi;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class A {

	public static void main(String[] args) {
		String sqlFile = "D:\\resource\\code\\study\\git\\g_shentong\\RuoYi-Vue\\sql2\\ry_vue_shentong_20240207.sql";
		String[] sequenceNames = { "SEQ_GEN_TABLE_COLUMN_ID", "SEQ_SYS_CONFIG_ID", "SEQ_SYS_DEPT_ID",
				"SEQ_SYS_DICT_DATA_ID", "SEQ_SYS_DICT_TYPE_ID", "SEQ_SYS_JOB_ID", "SEQ_SYS_JOB_LOG_ID",
				"SEQ_SYS_LOGININFOR_ID", "SEQ_SYS_MENU_ID", "SEQ_SYS_NOTICE_ID", "SEQ_SYS_OPER_LOG_ID",
				"SEQ_SYS_POST_ID", "SEQ_SYS_ROLE_ID", "SEQ_SYS_USER_ID" };

		replaceAutoIncrement(sqlFile, sequenceNames);
	}

	public static void replaceAutoIncrement(String sqlFile, String[] sequenceNames) {
		StringBuilder contentBuilder = new StringBuilder();
		try (BufferedReader br = new BufferedReader(new FileReader(sqlFile))) {
			String line;
			while ((line = br.readLine()) != null) {
				for (String sequenceName : sequenceNames) {
					String[] split = sequenceName.split("_");
					String colName = split[split.length - 2] + "_" + split[split.length - 1];
					if (line.indexOf("AUTO_INCREMENT") > -1) {
						if (line.indexOf(colName) > -1) {
							line = line.replaceAll("AUTO_INCREMENT", String.format("DEFAULT %s.NEXTVAL", sequenceName));
						}
					}
				}
				contentBuilder.append(line).append("\r\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		String sqlContent = contentBuilder.toString();
		for (String sequenceName : sequenceNames) {
			String[] split = sequenceName.split("_");
			String colName = split[split.length - 2] + "_" + split[split.length - 1];
			String pattern = "\\bAUTO_INCREMENT\\W*" + sequenceName + "\\b";
			String replacement = sequenceName + ".NEXTVAL";
			sqlContent = sqlContent.replaceAll(pattern, replacement);
		}
		System.out.print(sqlContent);
//		try (PrintWriter out = new PrintWriter(new FileWriter(sqlFile))) {
//			
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}
}
